% Function : recognize each monotone interval
% Parameter:
%   vec: data(vector)
%   d  : resolution
%   s  : step(optional)
% Return:
%   r: index of each section(vector)
function r = Section(vec, d, s)
    % check input parameter
    if ~isvector(vec)
        error('input must be a vector');
    end

    if nargin < 3
        % step length
        s = 4;
        if nargin < 2
            % resolution
            d = 0.2;
        end
    end

    vlen = numel(vec);

    % initialize
    k    = 1;
    r(k) = 1;
    st   = 1;
    ed   = -1;
    while ed + s <= vlen
        ed   = ed + s;
        subv = vec(st : ed);
        top  = max(subv);
        bot  = min(subv);

        if abs(top - bot) > d
            itop = find(top == subv);
            ibot = find(bot == subv);
            itop = itop(1);
            ibot = ibot(1);

            % initialize status
            stat = cmp(itop, ibot);

            if stat > 0
                st = st + itop;
                ed = st - 1;
            else
                st = st + ibot;
                ed = st - 1;
            end

            break;
        end
    end

    while ed + s < vlen && st < vlen
        % extend the search area until meet the requirement
        % requirement: |max-min| > d
        while ed + s <= vlen
            ed   = ed + s;
            subv = vec(st : ed);
            top  = max(subv);
            bot  = min(subv);

            % meet the requirement
            % look up to the section [st...ed]
            % determine whether it is monotone increasing or decreasing
            if abs(top - bot) > d
                itop = find(top == subv);
                ibot = find(bot == subv);
                itop = itop(1);
                ibot = ibot(1);

                cmpr = cmp(itop, ibot);

                % find the right endpoint of section
                if stat ~= cmpr
                    k = k + 1;
                    if stat > 0
                        if vec(st - 1) > top
                            r(k) = st - 1;
                        else
                            r(k) = st + itop - 1;
                        end
                    else
                        if vec(st - 1) < bot
                            r(k) = st - 1;
                        else
                            r(k) = st + ibot - 1;
                        end
                    end
                    stat = cmpr;
                end

                % udpate st of search area
                % new st is the next point of max or min in last search area
                if stat > 0
                    st = st + itop;
                    ed = st - 1;
                else
                    st = st + ibot;
                    ed = st - 1;
                end

                break;
            end
        end
    end

    % right endpoint of the last section
    r(k + 1) = vlen;
end

%% SubFunction %%
% compare method
function r = cmp(a, b)
    if a > b
        r = 1;
    elseif a < b
        r = -1;
    else
        r = 0;
    end
end
